package com.wee.aircoach_user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bigtotoro.util.SharedPreferencesUtil;
import com.wee.entity.User_detail;
import com.wee.fragment.tab_FragmentTest1;
import com.wee.fragment.tab_FragmentTest2;
import com.wee.fragment.tab_FragmentTest3;

/**
 * 体能自测
 */
public class PhysicalTestActivity extends FragmentActivity implements View.OnClickListener {
    //四个碎片 mTob3 /mTob4 是通过男女来区分的
    private tab_FragmentTest1 mTab01;
    private tab_FragmentTest2 mTab02;
    private tab_FragmentTest3 mTab03;
    //男女相别的判定 0为默认 男 ，1为女。
    private int tab_select=0;

    private LinearLayout mOne;
    private LinearLayout mTwo;
    private LinearLayout mThree;
    private FrameLayout tinengzice;

    private ImageView tab1_one,tab2_two,tab3_three,tab3_four;

    private String user_main;
    private User_detail data1;
    /**用于保存3个fragment之间的传递参数*/
    private FragmentArguments arguments =new FragmentArguments();

    /**用于在3个fragment之间的传递参数*/
    public class FragmentArguments{
        public Integer value0;
        public Integer value1;
    }
    public void setFragmentArguments(Bundle args){
        if(args.getInt("value0")!=0){
            arguments.value0=args.getInt("value0");

        }
        if(args.getInt("value1")!=0){
            arguments.value1=args.getInt("value1");
        }
    }
    public FragmentArguments setFragmentArguments(){
        return arguments;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
//这三个方法主要实现 点击跳转 的tab功能 以为 男女之分 所以就三个位置 mOne mTwo mThree 通过性别判定 实现哪个
     //   init();
        initViewPager();
        initEvent();
        setSelect(0);

    }

   /* private void init() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                gethttp();
            }
        }).start();
    }

    private void gethttp(){
        int  it=  SharedPreferencesUtil.getInt(this, Constant.USERID);
        user_main=Constant.USER_DETAIL+"&id="+it+"";
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, user_main,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {

                        String str = responseInfo.result;
                        Gson gs = new Gson();
                        JsonObject jo = gs.fromJson(str, JsonObject.class);
                        JsonElement je = jo.get("status");
                        String status = je.getAsString();
                        if ("ok".equals(status)) {
                            JsonObject object = jo.getAsJsonObject("data");
                            try {
                                data1 = gs.fromJson(object, User_detail.class);
                                String gender = data1.getGender();

                                *//**传递参数**//*
                            } catch (Exception r) {
                                Toast.makeText(PhysicalTestActivity.this, "请重新", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        System.out.println(e.getMessage());
                    }
                });

    }
*/
    private void setSelect(int i) {

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {
            case 0:
                tinengzice.setBackgroundResource(R.drawable.tab_f1);
                if (mTab01 == null) {
                    mTab01 = new tab_FragmentTest1();
                    transaction.add(R.id.id_content, mTab01);
                } else {
                    transaction.show(mTab01);
                }
                tab1_one.setBackgroundResource(R.drawable.heart_select);

                mTab01.setCallBack(new tab_FragmentTest1.CallBackToMain() {
                    @Override
                    public void callBackMain(boolean isTrue) {
                        if (isTrue) {
                            setSelect(1);
                            //恢复原来的颜色
                            tab1_one.setBackgroundResource(R.drawable.heart_no);
                        }
                    }
                });

                break;
            case 1:
                tinengzice.setBackgroundResource(R.drawable.tab_f2);
                if (mTab02 == null) {
                    mTab02 = new tab_FragmentTest2();
                    transaction.add(R.id.id_content, mTab02);
                } else {
                    transaction.show(mTab02);
                }
                tab2_two.setBackgroundResource(R.drawable.bearth_select);
                mTab02.setCallBack1(new tab_FragmentTest2.CallBackToMain1() {
                    @Override
                    public void callBackMain1(boolean isTrue) {
                        if(isTrue) {
                            setSelect(2);
                            tab2_two.setBackgroundResource(R.drawable.bearth_no);
                        }
                    }
                });

                break;
            case 2:
                tinengzice.setBackgroundResource(R.drawable.tab_f3);
                if(tab_select==0){
                    if (mTab03 == null) {
                        mTab03 = new tab_FragmentTest3();
                        transaction.add(R.id.id_content, mTab03);
                    } else {
                        transaction.show(mTab03);
                    }
                    tab3_three.setBackgroundResource(R.drawable.fu_select);
                }else if(tab_select==1){
                    if (mTab03 == null) {
                        mTab03 = new tab_FragmentTest3();
                        transaction.add(R.id.id_content, mTab03);
                    } else {
                        transaction.show(mTab03);
                    }
                    tab3_four.setBackgroundResource(R.drawable.yang_select);
                }
                break;
            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01 != null) {
            transaction.hide(mTab01);
        }
        if (mTab02 != null) {
            transaction.hide(mTab02);
        }
        if (mTab03 != null) {
            transaction.hide(mTab03);
        }
    }

    private void initEvent() {
        mOne.setOnClickListener(this);
        mTwo.setOnClickListener(this);
        mThree.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId()) {
            case R.id.id_tab_one:
                setSelect(0);
                break;
            case R.id.id_tab_two:
                setSelect(1);
                break;
            case R.id.id_tab_three:
                setSelect(2);
                break;
            case R.id.back:
                startActivity(new Intent(PhysicalTestActivity.this, MainActivity.class));
                finish();
            default:
                break;
        }
    }
    private void resetImgs() {
        tab1_one.setBackgroundResource(R.drawable.heart_no);
        tab2_two.setBackgroundResource(R.drawable.bearth_no);

        tab3_three.setBackgroundResource(R.drawable.fu_no);
        tab3_four.setBackgroundResource(R.drawable.yang_no);


    }
    private void initViewPager() {
        /**选项卡的 标提头和 传值跳转
         经过考虑 需要传递的值至少有三个
         跳转确认值 状态完成验证 传递之间的测试结果 m为 f1.结果
         然后在第三个 选项卡上完成跳转到结果呈现页
         之前需区分 男女的动作即 俯卧撑（f3）或者 仰卧起坐（f4）
         male == 男
         fmale == 女
         */

        mOne = (LinearLayout) findViewById(R.id.id_tab_one);
        mTwo = (LinearLayout) findViewById(R.id.id_tab_two);
        mThree = (LinearLayout) findViewById(R.id.id_tab_three);

        tinengzice=(FrameLayout)findViewById(R.id.bottom_zice);

        tab1_one=(ImageView)findViewById(R.id.tab1_one);
        tab2_two=(ImageView)findViewById(R.id.tab2_two);
        tab3_three=(ImageView)findViewById(R.id.tab3_three);
        tab3_four=(ImageView)findViewById(R.id.tab3_four);


        findViewById(R.id.back).setOnClickListener(this);
        try{
            String  user_gender=SharedPreferencesUtil.get(this,Constant.GENDER);
            if(user_gender!=null){
                if(user_gender.equals("male")){
                    tab_select=0;
                    tab3_three.setVisibility(View.VISIBLE);
                    tab3_four.setVisibility(View.GONE);

                }else if (user_gender.equals("female")){
                    tab_select=1;
                    tab3_three.setVisibility(View.GONE);
                    tab3_four.setVisibility(View.VISIBLE);
                }
            }
        }catch (Exception r){}
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK ){
            startActivity(new Intent(PhysicalTestActivity.this, MainActivity.class));
            finish();
        }
        return super.onKeyDown(keyCode, event);

    }
}
